#ifndef IFINDSUPPORT_H
#define IFINDSUPPORT_H

#include <QtCore/QObject>
#include <QtCore/QString>
#include <QtGui/QTextDocument>

namespace Find {

class IFindSupport : public QObject
{
    Q_OBJECT

public:
    // Compatible with QTextDocument::FindFlags, plus `FindRegularExpression`
    enum FindFlag {
        FindBackward = 0x01,
        FindCaseSensitively = 0x02,
        FindWholeWords = 0x04,
        FindRegularExpression = 0x08
    };
    Q_DECLARE_FLAGS(FindFlags, FindFlag)

    enum Result {
        Found,
        NotFound,
        NotYetFound
    };

    IFindSupport() : QObject(0) { }
    virtual ~IFindSupport() { }

    virtual bool supportsReplace() const = 0;
    virtual FindFlags supportedFindFlags() const = 0;
    virtual void resetIncrementalSearch() = 0;
    virtual void clearResults() = 0;
    virtual QString currentFindString() const = 0;
    virtual QString completedFindString() const = 0;

    virtual void highlightAll(const QString &txt, FindFlags findFlags);
    virtual Result findIncremental(const QString &txt, FindFlags findFlags) = 0;
    virtual Result findStep(const QString &txt, FindFlags findFlags) = 0;
    virtual bool replaceStep(const QString &before, const QString &after,
        FindFlags findFlags) = 0;
    virtual int replaceAll(const QString &before, const QString &after,
        FindFlags findFlags) = 0;

    virtual void defineFindScope() { }
    virtual void clearFindScope() { }

    static QTextDocument::FindFlags textDocumentFlagsForFindFlags(IFindSupport::FindFlags flags)
    {
        QTextDocument::FindFlags textDocFlags;
        if (flags & IFindSupport::FindBackward)
            textDocFlags |= QTextDocument::FindBackward;
        if (flags & IFindSupport::FindCaseSensitively)
            textDocFlags |= QTextDocument::FindCaseSensitively;
        if (flags & IFindSupport::FindWholeWords)
            textDocFlags |= QTextDocument::FindWholeWords;
        return textDocFlags;
    }

signals:
    void changed();
};

inline void IFindSupport::highlightAll(const QString &, FindFlags) { }

} // namespace Find

Q_DECLARE_OPERATORS_FOR_FLAGS(Find::IFindSupport::FindFlags)

#endif // IFINDSUPPORT_H
